﻿using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using GUIServerConnector;
using Server.TcpIp;
using ApplicationConfiguration;
using EntityModel;
using SensorClient;
using Logging;

/*
GUIServerConnectorController.startServices();
System.Console.WriteLine("GUIServerConnector started. Press <return> to exit...");
System.Console.ReadLine();
*/
namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                bool done = false;
                Server server = new Server();
                server.start();
                Console.Out.WriteLine("Type \"help\" for a list of available commands");
                while (!done)
                {
                    string command = Console.In.ReadLine().ToLower();
                    if (command.Equals("shutdown"))
                    {
                        done = true;
                        server.stop();
                    }
                    else if (command.Equals("status"))
                    {
                        dumpStatus(server, DateTime.Now, "");
                    }
                    else if (command.Equals("restart"))
                    {
                        server.stop();
                        server.start();
                    }
                    else if (command.Equals("help"))
                    {
                        Console.WriteLine("\tstatus - displays status information for various server endpoints");
                        Console.WriteLine("\trestart - performs a soft restart of the server");
                        Console.WriteLine("\tshutdown - performs a soft shutdown of the server");
                        Console.WriteLine("\thelp - displays this message");
                    }
                    else
                    {
                        Console.Out.WriteLine("Invalid command \"" + command + "\"!");
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                Console.Out.WriteLine("Press <return> to exit...");
                Console.In.ReadLine();
                throw e;
            }
        }

        private static void dumpStatus(StatusProvider statusProvider, DateTime currentTime, string indent)
        {
            Console.Out.WriteLine(indent + statusProvider.GetType().FullName
                + ": Uptime = " + (currentTime.Subtract(statusProvider.getStartTime()))
                + ", LastAccessed = " + (statusProvider.getLastAccessTime().ToLongDateString() + " at " + statusProvider.getLastAccessTime().ToLongTimeString()));
            foreach (StatusProvider childStatusProvider in statusProvider.getChildren())
            {
                dumpStatus(childStatusProvider, currentTime, indent + "\t");
            }
        }
    }
}
